package com.xxxx.server.config.security.quanxian;
import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import java.util.Collection;
@Component
public class CustomUrlDecisionManager implements AccessDecisionManager {
    @Override
    public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {
        for (ConfigAttribute configAttribute:configAttributes){
            String needrole=configAttribute.getAttribute();
            if ("ROLE_LOGIN".equals(needrole)){
                if (authentication instanceof AnonymousAuthenticationToken){
                    throw new AccessDeniedException("尚未登陆，请登录");
                }else {
                    return;
                }
            }
            Collection<? extends GrantedAuthority> calendars= authentication.getAuthorities();
            for (GrantedAuthority grantedAuthority:calendars){
                if (grantedAuthority.getAuthority().equals(needrole)){
                    return;
                }
            }
        }
        throw new AccessDeniedException("权限不足，联系管理员");
    }
    @Override
    public boolean supports(ConfigAttribute attribute) {
        return false;
    }
    @Override
    public boolean supports(Class<?> clazz) {
        return false;
    }
}
